package top.infopub.util;


import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.infopub.common.ErrorCodeEnum;
import top.infopub.vo.ResponseVo;


/**
 * 邮件发送工具类 <br/>
 * 注意：此方法主要用于各dubbo-provider发送服务邮件告警， 其他业务务必使用MGC的邮件服务
 * @author Awoke
 * @version 2018年11月7日
 * @see EmailSendUtil
 * @since
 */
public class EmailSendUtil {

    private static final Logger logger = LoggerFactory.getLogger(EmailSendUtil.class);

    /**
     * 发送html邮件
     * @param cfg   EmailSendUtil.MailCfg
     * @param title   标题
     * @param content 内容
     * @return 
     * @see
     */
    @SuppressWarnings("unchecked")
    public static ResponseVo<String> htmlMail(MailCfg cfg, String title, String content) {
        try {
            HtmlEmail email = new HtmlEmail();
            email.setCharset("UTF-8");
            email.setHostName(cfg.getHost());
            // 阿里云ECS为管控垃圾邮件，屏蔽了25端口号的服务，所以导致该访问超时，尝试使用ssl方式的465端口
            email.setSmtpPort(465);
            email.setSSLOnConnect(true);
            email.setAuthentication(cfg.getAccName(), cfg.getAccPwd());
            email.setFrom(cfg.getAccName(), cfg.getFromName());
            String emailTo = cfg.getEmailTo();
            if (StringUtils.contains(emailTo, ",")) { // 多个收件人
                email.addTo(emailTo.split(","));
            }
            else { // 单个收件人      
                email.addTo(emailTo);
            }
            email.setSubject(title);
            email.setHtmlMsg(content);
            // send
            email.send();
            return ResponseVo.getSuccessResponse();
        }
        catch (Exception e) {
            logger.error("sendMail error", e);
            return ErrorCodeEnum.ERROR_MGC_SENDMAIL.getResponseVo(e.getClass().getName() + " "
                                                                  + e.getMessage());
        }
    }

    public static class MailCfg {

        String host; // host

        String accName; // 账户

        String accPwd; // 密码

        String fromName; // 发送方名称

        String emailTo; // 接收方名称，多个用英文逗号分隔

        public MailCfg() {

        }

        public MailCfg(String host, String accName, String accPwd, String fromName, String emailTo) {
            super();
            this.host = host;
            this.accName = accName;
            this.accPwd = accPwd;
            this.fromName = fromName;
            this.emailTo = emailTo;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public String getAccName() {
            return accName;
        }

        public void setAccName(String accName) {
            this.accName = accName;
        }

        public String getAccPwd() {
            return accPwd;
        }

        public void setAccPwd(String accPwd) {
            this.accPwd = accPwd;
        }

        public String getFromName() {
            return fromName;
        }

        public void setFromName(String fromName) {
            this.fromName = fromName;
        }

        public String getEmailTo() {
            return emailTo;
        }

        public void setEmailTo(String emailTo) {
            this.emailTo = emailTo;
        }

        @Override
        public String toString() {
            return "MailCfg [host=" + host + ", accName=" + accName + ", accPwd=" + accPwd
                   + ", fromName=" + fromName + ", emailTo=" + emailTo + "]";
        }

    }

}
